package com.test.daily.leetcode.y2022.m07.day0723.v02;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/7/23 下午 04:23
 * @version: 1.0
 */
public class Solution {
    public static int maxProfile(int[] profiles, int[] costs, int W, int K){
        PriorityQueue<Program> minCost = new PriorityQueue<>(new CostComparator());
        PriorityQueue<Program> maxProfile = new PriorityQueue<>(new ProfileComparator());
        for (int i = 0; i < costs.length; i++) {
            minCost.add(new Program(costs[i], profiles[i]));
        }
        int w = W;
        for (int i = 0; i < K; i++) {
            while(!minCost.isEmpty() && minCost.peek().cost <= w){
                maxProfile.add(minCost.poll());
            }
            if(maxProfile.size() == 0){
                return w;
            }
            w += maxProfile.poll().profile;
        }
        return w;
    }

    public static class CostComparator implements Comparator<Program>{

        @Override
        public int compare(Program o1, Program o2){
            return o1.cost- o2.cost;
        }
    }
    public static class ProfileComparator implements Comparator<Program>{
        @Override
        public int compare(Program o1, Program o2){
            return o2.profile - o1.profile;
        }
    }
    public static class Program{
        int cost;
        int profile;
        public Program(int cost, int profile){
            this.cost = cost;
            this.profile = profile;
        }
    }
}
